/*
 * @lc app=leetcode.cn id=289 lang=c
 *
 * [289] 生命游戏
 */

// @lc code=start


void gameOfLife(int** board, int boardSize, int* boardColSize){
    int res[boardSize][boardColSize[0]];
    int dir[8][2] = {{0,1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
    for (int i = 0; i < boardSize; ++i)
    {
        for (int j = 0; j < boardColSize[0]; ++j)
        {
            int cnt = 0;
            for (int k = 0; k < 8; ++k)
            {
                int x = dir[k][0] + i;
                int y = dir[k][1] + j;
                if (0 <= x && x < boardSize && 0 <= y && y < boardColSize[0] && board[x][y])
                {
                    cnt++;
                }
            }
            if (board[i][j]) 
            { 
                res[i][j] = (2 <= cnt && cnt <= 3) ? 1 : 0;
            } else {
                res[i][j] = (cnt == 3) ? 1 : 0;
            }
        }
    }
    for (int i = 0; i < boardSize; ++i)
    {
        for (int j = 0; j < boardColSize[0]; ++j)
        {
            board[i][j] = res[i][j];
        }
    }
}
// @lc code=end

